package org.oos.services.util;

import org.oos.data.Area;
import org.oos.data.Location;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class AtomUtils
{
	/**
	 * Parses an oos location from a given entry.
	 * 
	 * @param entry
	 * @return
	 */
	public Location parseLocation(Element entry)
	{
		Location location = new Location();
		Element locality = (Element) entry.getElementsByTagName("oos:locality").item(0);
		String localityName = locality.getChildNodes().item(0).getNodeValue();
		String localitySlug = locality.getChildNodes().item(0).getNodeValue();
		location.locality = new Area(localityName, localitySlug);
		location.address = getElementValue(entry, "oos:useraddress");
		String coordinates = getElementValue(entry, "gml:pos");
		if (coordinates!=null)
		{
			String[] coord = coordinates.split(" ");
			location.latitude = Double.parseDouble(coord[0]);
			location.longitude = Double.parseDouble(coord[1]);
		}
		return location;
	}
	
	/**
	 * Searches the element <code>e</code> for the first element named <code>elementName</code>
	 * and returns its value as a String.
	 * 
	 * @param e
	 * @param elementName
	 * @return element value or null if the element is not found
	 */
	public String getElementValue(Element e, String elementName)
	{
		NodeList nl = e.getElementsByTagName(elementName);
		if (nl != null && nl.getLength() > 0)
		{
			//return nl.item(0).getChildNodes().item(0).getNodeValue();
			return getValue(nl.item(0));
		}
		else
		{
			return null;
		}
	}
	
	/**
	 * Searches the element <code>e</code> for the first child element named <code>elementName</code>
	 * and with a rel value = <code>rel</code> and returns the value of its href attribute as a String.
	 * 
	 * @param e
	 * @param elementName
	 * @param rel
	 * @return element value or null if the element is not found
	 */
	public String getElementValue(Element e, String elementName, String rel)
	{
		NodeList nl = e.getElementsByTagName(elementName);
		if (nl != null && nl.getLength() > 0)
		{
			for (int i=0; i<nl.getLength(); i++)
			{
				Node node = nl.item(i);
				if (node.getAttributes()!=null) {
					Node relNode = node.getAttributes().getNamedItem("rel");
					if (relNode!=null) {
						String relValue = relNode.getNodeValue();
						if (rel.equals(relValue)) {
							Node hrefNode = node.getAttributes().getNamedItem("href");
							return hrefNode.getNodeValue(); 
						}
					}
				}
			}
			return null;
		}
		else
		{
			return null;
		}
	}
	
	public String getElementHrefValue(Element e, String elementName)
	{
		NodeList nl = e.getElementsByTagName(elementName);
		if (nl != null && nl.getLength() > 0)
		{
			for (int i=0; i<nl.getLength(); i++)
			{
				Node node = nl.item(i);
				if (node.getAttributes()!=null) {
					Node hrefNode = node.getAttributes().getNamedItem("href");
					if (hrefNode!=null) return hrefNode.getNodeValue(); 
				}
			}
			return null;
		}
		else
		{
			return null;
		}
	}
	
	/**
	 * Returns the value of node <code>n</code>.
	 * Gets: <element>_value_</element> Returns: _value_
	 * @param n
	 * @return
	 */
	public String getValue(Node n)
	{
		String value = "";
		try {
			value = n.getChildNodes().item(0).getNodeValue();
		} catch (Exception e) {
			// do nothing
		}
		return value;
	}
}
